package cn.lds.healthj377.controller;

import cn.lds.healthj377.domain.AppointmentRecord;
import cn.lds.healthj377.domain.User;
import cn.lds.healthj377.dto.Pager;
import cn.lds.healthj377.dto.ResponseDTO;
import cn.lds.healthj377.exception.SysException;
import cn.lds.healthj377.service.AppointmentRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("appointment_record")
public class AppointmentController {

    private static final Logger log = LoggerFactory.getLogger(AppointmentController.class);
    @Autowired
    private AppointmentRecordService appointmentRecordService;

    @RequestMapping(value = "my_appoint",method = RequestMethod.GET)
    public String queryMyAppoint(HttpSession session, Model model) throws SysException {

        AppointmentRecord appointmentRecord = new AppointmentRecord();

        User loginUser = (User) session.getAttribute("loginUser");

        appointmentRecord.setUser(loginUser);
        ResponseDTO<List<AppointmentRecord>> listResponseDTO = appointmentRecordService.selectAppointmentRecordByCondition(appointmentRecord,null);

        model.addAttribute("result",listResponseDTO);

        return "show_my_appoint";

    }
    @RequestMapping("/show_appoint_list")
    public String showAppointAll(AppointmentRecord appointmentRecord, Model model, Pager pager) throws SysException {

        log.info("预约:{}",appointmentRecord);
        log.info("分页:{}",pager);
        model.addAttribute("pager",pager);
        ResponseDTO<List<AppointmentRecord>> listResponseDTO = appointmentRecordService.selectAppointmentRecordByCondition(appointmentRecord,pager);

        model.addAttribute("result",listResponseDTO);




        return "show_appoint";
    }

    @RequestMapping("do_appoint")
    public String addAppoint(AppointmentRecord appointmentRecord,HttpSession session) throws SysException {
        //获取登录用户
        User loginUser = (User) session.getAttribute("loginUser");
        //获取登录用户id
        Long id = loginUser.getId();

        appointmentRecord.setUserId(id);
        //将Long类型转成Integer
        Integer userId = Math.toIntExact(id);

        appointmentRecord.setCreateBy(userId);

        log.info("预约信息:{}",appointmentRecord);

        ResponseDTO responseDTO = appointmentRecordService.addAppoint(appointmentRecord);
        //添加预约后，返回到查询我的预约页面
        return "redirect:my_appoint";

    }

    @RequestMapping("confirm/{id}")
    public String appointConfirm(@PathVariable Integer id){

        appointmentRecordService.confilmAppoint(id);

        //重定向会显示预约页面
        return "redirect:/appointment_record/show_appoint_list";

    }
}
